package com.guiwu.gteway.industryapp.Data;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.guiwu.gteway.industryapp.Api.GuiwuApi;
import com.guiwu.gteway.industryapp.Api.GuiwuApiCallback;
import com.guiwu.gteway.industryapp.Device.module.GateWay;
import com.guiwu.gteway.industryapp.Device.module.User;
import com.guiwu.gteway.industryapp.ExecutorUtil.AppExecutors;
import com.guiwu.gteway.industryapp.util.ApiJsonUtil;
import com.guiwu.gteway.industryapp.util.JsonResult;
import com.guiwu.gteway.industryapp.util.MyLog;

import static com.google.common.base.Preconditions.checkNotNull;

public class DataRepository implements MDataSource {

    private User mUser = null;
    private GateWay mCurrentGateWay = null;
    private AppExecutors appExecutors;

    private DataRepository(){
        appExecutors = new AppExecutors();
    }

    private static class DataRepositoryHolder{
        private final static DataRepository instance = new DataRepository();
    }

    public static DataRepository getInstance(){
        return DataRepositoryHolder.instance;
    }

    //获得用户信息
    @Override
    public void getUserInfo(String userId,final GetUserInfoCallback callback) {
        checkNotNull(userId);
        checkNotNull(callback);
        if (mUser == null){
            GuiwuApi.getUserInfo(userId, new GuiwuApiCallback() {
                @Override
                public void onSuccess(String response) {
                    JsonResult result =  ApiJsonUtil.getData(response);
                    if (result.getJsonElement() != null){
                        Gson gson = new Gson();
                        try{
                            MyLog.i("MainActivity",result.getJsonElement().toString());
                            JsonObject object = result.getJsonElement().getAsJsonArray().get(0).getAsJsonObject();
                            User user = gson.fromJson(object,User.class);
                            callback.onUserInfo(user);
                            mUser = user;
                        }catch (JsonSyntaxException ex){
                            callback.onDataNotAvailable(ex.toString());
                        }
                    }else {
                        callback.onDataNotAvailable(result.getErrorMsg());
                    }
                }

                @Override
                public void onFailed(Exception e) {
                    callback.onDataNotAvailable(e.getMessage());
                }
            });
        }else {
            callback.onUserInfo(mUser);
        }
    }

    //获取网关分享者名称
    @Override
    public void getDeviceShareName(String sn, final DeviceShareNameCallback callback) {
        checkNotNull(sn);
        checkNotNull(callback);
        if (mCurrentGateWay == null){
            callback.onDataNotAvailable("gateway不应该为null,请在获取到具体网关后赋值");
        }else if (mCurrentGateWay.getShareName() == null){
            GuiwuApi.getGateWayShareName(sn, new GuiwuApiCallback() {
                @Override
                public void onSuccess(String response) {
                    JsonResult result =  ApiJsonUtil.getData(response);
                    if (result.getJsonElement() != null){
                        try{
                            JsonObject object = result.getJsonElement().getAsJsonArray().get(0).getAsJsonObject();
                            String userName = object.get("userName").getAsString();
                            mCurrentGateWay.setShareName(userName);
                            callback.onDeviceINfo(mCurrentGateWay);
                        }catch (JsonSyntaxException ex){
                            callback.onDataNotAvailable(ex.getMessage());
                        }
                    }else {
                        callback.onDataNotAvailable(result.getErrorMsg());
                    }
                }

                @Override
                public void onFailed(Exception e) {
                    callback.onDataNotAvailable(e.getMessage());
                }
            });
        }else {
            callback.onDeviceINfo(mCurrentGateWay);
        }
    }

    //获取网关连接类型
    @Override
    public void getDeviceConnectType(String sn,final DeviceConnectTypeCallback callback) {
        checkNotNull(sn);
        checkNotNull(callback);
        if (mCurrentGateWay == null){
            callback.onDataNotAvailable("gateway不应该为null,请在获取到具体网关后赋值");
        }else if(mCurrentGateWay.getConnectType() == null){
            GuiwuApi.getGateWayConnectType(sn, new GuiwuApiCallback() {
                @Override
                public void onSuccess(String response) {
                    JsonResult result =  ApiJsonUtil.getData(response);
                    if (result.getJsonElement() != null){
                        try{
                            JsonObject object = result.getJsonElement().getAsJsonArray().get(0).getAsJsonObject();
                            String connectType = object.get("connectType").getAsString();
                            mCurrentGateWay.setConnectType(connectType);
                            callback.onDeviceInfo(mCurrentGateWay);
                        }catch (JsonSyntaxException ex){
                            callback.onDataNotAvailable(ex.getMessage());
                        }
                    }else {
                        callback.onDataNotAvailable(result.getErrorMsg());
                    }
                }

                @Override
                public void onFailed(Exception e) {
                    callback.onDataNotAvailable(e.getMessage());
                }
            });
        }else {
            callback.onDeviceInfo(mCurrentGateWay);
        }
    }


    //设置gateWay
    public void setmCurrentGateWay(GateWay gateWay){
        mCurrentGateWay = gateWay;
    }

    public void clearUser(){
        mUser = null;
    }

    public void clearCurrentGateWay(){
        mCurrentGateWay = null;
    }
}
